﻿#include "CursorEditEntry.h"
#include <QComboBox>

struct CursorEditEntry::CursorEditEntryPrivate {
    QPointer<QComboBox> combobox;
};

CursorEditEntry::~CursorEditEntry() { }

CursorEditEntry::CursorEditEntry(QWidget *parent) : EditEntry{parent} {
    p.reset(new CursorEditEntryPrivate);
    initUi();
}

void CursorEditEntry::setValue(const QCursor &value) {
    for(int i = 0; i < p->combobox->count(); ++i) {
        const auto &itemValue = Qt::CursorShape(p->combobox->itemData(i).toInt());
        if(itemValue == value.shape()) {
            p->combobox->setCurrentIndex(i);
            break;
        }
    }
}

QCursor CursorEditEntry::value() const {
    const auto &shape = Qt::CursorShape(p->combobox->currentData().toInt());
    return QCursor(shape);
}

void CursorEditEntry::initUi() {
    p->combobox = new QComboBox;
    addEditControll(p->combobox);

    const auto &cursorEnum =  QMetaEnum::fromType<Qt::CursorShape>();
    for(int i = 0; i < cursorEnum.keyCount(); ++i) {
        const auto &shape = Qt::CursorShape(cursorEnum.value(i));
        QIcon icon(QCursor(shape).pixmap());
        p->combobox->addItem(icon, cursorEnum.key(i), cursorEnum.value(i));
    }

    connect(p->combobox.data(), qOverload<int>(&QComboBox::currentIndexChanged), [=] {
        emit valueChanged(value());
    });
}
